<script>
import { getDepartment, getManagerList, addDepartment, getDepartmentDetail, updateDepartment,delDepartment } from '@/api/department'
export default {
  name: 'DialogAddDepartment',
  props: {
    currentNodeId: {
      type: Number,
      default: null
    },
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '新增部门'
    }
  },
  data() {
    return {
      managerList: [],
      dialogFormVisible: false,
      formData: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人id
        name: '', // 部门名称
        pid: '' // 父级部门的id
      },
      rules: {
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' }, { min: 2, max: 10 }, {
          trigger: 'blur',
          //   自定义校验模式
          validator: async(rule, value, callback) => {
            let result = await getDepartment()
            if (this.formData.id) {
              // 编辑场景下排除自身
              result = result.filter(item => item.id !== this.formData.id)
            }
            if (result.some(item => value === item.code)) {
              callback(new Error('部门编码重复了'))
            } else {
              callback()
            }
          }
        }], // 部门编码
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }, { min: 2, max: 100, message: '部门介绍必须2到100个字符' }], // 部门介绍
        managerId: [], // 部门负责人id
        name: [{ required: true, message: '部门名字不能为空', trigger: 'blur' }, { min: 1, max: 100 }, {
          trigger: 'blur',
          validator: async(rule, value, callback) => {
            let result = await getDepartment()
            if (this.formData.id) {
              // 编辑场景下排除自身
              result = result.filter(item => item.id !== this.formData.id)
            }
            if (result.some(item => value === item.name)) {
              callback(new Error('部门名称重复了'))
            } else {
              callback()
            }
          }
        }] // 部门名称
      }
    }
  },
  watch: {
    visible: {
      handler(val) {
        this.dialogFormVisible = val
      },
      immediate: true
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    close() {
      // 重置表单
      this.formData = {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人id
        name: '', // 部门名称
        pid: '' // 父级部门的id
      }
      this.$refs.addDept.resetFields()
      this.$emit('update:visible', false)
    },
    async getManagerList() {
      this.managerList = await getManagerList()
    },
    confirm() {
      // 点击确定 ==》表单验证 ===》添加部门接口 ===》 通知父组件更新数据 ===》重置表单 ===》关闭弹层
      this.$refs.addDept.validate(async valid => {
        if (valid) {
          let msg = ''
          if (this.formData.id) {
            await updateDepartment({ ...this.formData })
            msg = '更新'
          } else {
            await addDepartment({ ...this.formData, pid: this.currentNodeId })
            msg = '新增'
          }
          // 通知父组件更新数据
          this.$emit('updateDepartment')
          this.$message(`${msg}部门成功`)
          this.close()
        }
      })
    },
    async getDepartmentDetail() {
      this.formData = await getDepartmentDetail(this.currentNodeId)
    }

  }

}
</script>

<template>
  <div class="department-container">
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%" @close="close">
      <el-form ref="addDept" label-position="right" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" style="width:80%;" placeholder="2-10个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formData.code" style="width: 80%" placeholder="2-10个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="managerId">
          <el-select v-model="formData.managerId" style="width: 80%" placeholder="请选择负责人">
            <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce" style="width: 80%" type="textarea" :rows="4" placeholder="1-100个字符" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="confirm">确定</el-button>
              <el-button size="mini" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">

</style>
